home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 213_01 / batchr.c < prev    next >
Text File  |  1980-01-01  |  7KB  |  328 lines

  1. /* BATCHR.C    VERS:- 01.00  DATE:- 09/26/86  TIME:- 09:35:57 PM */
  2. /*
  3. %CC1 $1.C -O -X -E5680
  4. %CLINK $1 DIO WILDEXP -S -E5680
  5. %DELETE $1.CRL 
  6. */
  7. /* 
  8. Description:
  9.  
  10. Submit utility BATCH from Van Nuys Toolkit, by Eugene H. Mallory.
  11.  
  12. Minor modifications:
  13.     converted to standard C tokens;
  14.     extended menu to include all BAT, ZEX, AND SUB files in all 
  15.         user areas of current drive and of drive A:, 
  16.         if no valid file given on command line;
  17.     error exit if no files found.
  18.  
  19. By J.A. Rupley, Tucson, Arizona
  20. Coded for BDS C compiler, version 1.50a
  21. */
  22. /*********************************************************************
  23. *                               BATCH                                *
  24. **********************************************************************
  25. *                  COPYRIGHT 1983 EUGENE H. MALLORY                  *
  26. *********************************************************************/
  27. #include "BDSCIO.H"
  28. #include "DIO.H"
  29. #define BCOUNT 66
  30. char string[MAXLINE];
  31. char fname[MAXLINE];
  32. char c;
  33. int fd1, fd2;
  34. char fcb1[BUFSIZ], fcb2[BUFSIZ];
  35. char barray[BCOUNT][80];
  36. char buffer[128];
  37. int bctr, flag;
  38. int string_compare();
  39.  
  40. main(argc, argv)
  41. char **argv;
  42. int argc;
  43.  
  44. {
  45.     int i, j, menuflag;
  46.     char *fvector[7];
  47.     char **fvectorp;
  48.     char *disk;
  49.     int diskno, vctr, len;
  50.  
  51.     dioinit(&argc, argv);
  52.  
  53.     if (DIOIN)
  54.     {
  55.         bctr = 0;
  56.         while (!getstring(string))
  57.         {
  58.             len = strlen(string);
  59.             string[len - 1] = 0;
  60.             linecopy(barray[bctr++], string, argv, argc, 0);
  61.             if (bctr == BCOUNT)
  62.                 error("BATCH: Batch file too big.");
  63.         }
  64.         goto writesub;
  65.     }
  66.  
  67.  
  68.     if (argc >= 2 && argv[1][0] == '/')
  69.     {
  70.         bctr = 0;
  71.         typef("*");
  72.         while (gets(string))
  73.         {
  74.             if (string[0] == 0)
  75.                 goto writesub;
  76.             linecopy(barray[bctr++], string, argv, argc, 1);
  77.             if (bctr == BCOUNT)
  78.                 error("BATCH: Batch file too big.");
  79.             typef("*");
  80.         }
  81.         goto writesub;
  82.     }
  83.  
  84.     if (argc < 2)
  85.     {
  86.         disk = "ABCDEFGHIJKL";
  87.         vctr = 7;
  88.         diskno = BDOS(25);
  89.         if (!diskno)
  90.             vctr = 4;
  91.         fvector[0] = "DUMMY";
  92.         fvector[1] = "??:*.BAT";
  93.         fvector[2] = "??:*.ZEX";
  94.         fvector[3] = "??:*.SUB";
  95.         fvector[1][0] = disk[diskno];
  96.         fvector[2][0] = disk[diskno];
  97.         fvector[3][0] = disk[diskno];
  98.         fvector[4] = "A?:*.BAT";
  99.         fvector[5] = "A?:*.ZEX";
  100.         fvector[6] = "A?:*.SUB";
  101.         fvectorp = fvector;
  102.         wildexp(&vctr, &fvectorp);
  103.         if (vctr < 2)
  104.             error("\nNo BAT, ZEX, or SUB files found on drives A: or %c:\n", disk[diskno]);
  105.         qsort(&fvectorp[1], vctr - 1, 2, &string_compare);
  106.         fvectorp++;
  107. filemenu :
  108.         i = menu(vctr - 1, fvectorp);
  109.         if (i == -1)
  110.             goto exitbatch;
  111.         strcpy(fname, fvectorp[i]);
  112.         fd1 = fopen(fname, fcb1);
  113.         if (fd1 == ERROR)
  114.             error("BATCH: Unable to open %s", fname);
  115.     }
  116.     else
  117.         {
  118.         strcpy(fname, argv[1]);
  119.         strcat(fname, ".BAT");
  120.         fd1 = fopen(fname, fcb1);
  121.         if (fd1 == ERROR)
  122.         {
  123.             strcpy(fname, "a:");
  124.             strcat(fname, argv[1]);
  125.             strcat(fname, ".BAT");
  126.             fd1 = fopen(fname, fcb1);
  127.         }
  128.         if (fd1 == ERROR)
  129.         {
  130.             dioflush();
  131.             error("BATCH: Unable to find: %s. \N", fname);
  132.         }
  133.     }
  134.  
  135.     if (!fgets(string, fcb1))
  136.     {
  137.         dioflush();
  138.         error("BATCH: Batch file %s empty.", fname);
  139.     }
  140.  
  141.     bctr = 0;
  142.     do
  143.         {
  144.         len = strlen(string);
  145.         string[len - 1] = 0;
  146.         linecopy(barray[bctr++], string, argv, argc, 1);
  147.         if (bctr == BCOUNT)
  148.             error("BATCH: Batch file too big.");
  149.     }
  150.     while (fgets(string, fcb1))
  151.         ;
  152.  
  153.     /*    SUBSTITUTED ARGUMENTS ARE NOW IN BARRAY */
  154.  
  155. writesub :
  156.     fd2 = open("A:$$$.SUB", 2);
  157.     if (fd2 == -1)
  158.     {
  159.         fd2 = creat("A:$$$.SUB");
  160.     }
  161.     else
  162.     {
  163.         do
  164.             {
  165.             flag = read(fd2, buffer, 1);
  166.             if (flag == -1)
  167.                 error("BATCH: Unable to append to A:$$$.SUB.");
  168.         }
  169.         while (flag)
  170.             ;
  171.     }
  172.  
  173.     /* Now $$.sub should be opened correctly */
  174.  
  175.     while (bctr--)
  176.     {
  177.         for (i = 0; i < 128; i++)
  178.             buffer[i] = 0x1a;
  179.         strcpy(&buffer[1], barray[bctr]);
  180.         buffer[0] = strlen(barray[bctr]);
  181.         i = write(fd2, buffer, 1);
  182.         if (i != 1)
  183.             error("BATCH: Unable to write A:$$$.SUB.");
  184.     }
  185.     close(fd2);
  186.  
  187. exitbatch :
  188.     dioflush();
  189.     bios(1);
  190. }
  191.  
  192. menu(icounter, strings)
  193. char **strings;
  194. int icounter;
  195. {
  196.     int max, bias, i, j;
  197.     char *prompt;
  198.     prompt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  199.     bias = 0;
  200. xxxx :
  201.     typef("\n\0x1b*");
  202.     if ((icounter - bias) > 26)
  203.         max = 26;
  204.     else
  205.         max = icounter - bias;
  206.     for (i = 0; i < (max + 1) / 2; i++)
  207.     {
  208.         typef("%c) %s", prompt[i], strings[i + bias]);
  209.         if (i + bias + (max + 1) / 2 < icounter)
  210.         {
  211.             for (j = 0; j <= 37 - strlen(strings[i + bias]); j++)
  212.                 typef(" ");
  213.             typef("%c) %s\n",
  214.             prompt[i + (max + 1) / 2],
  215.             strings[i + bias + (max + 1) / 2]);
  216.         }
  217.         else
  218.             typef("\n");
  219.     }
  220.     typef("\n\n");
  221.     if (icounter > bias + 26)
  222.         typef("More selections are available.\n");
  223.     typef("Type selection, ESC to exit, CR for more, - to backup :");
  224.     c = bdos(1);
  225.     typef("\n\014");
  226.     if (c == '\033')
  227.         return -1;
  228.     if (c == '\r' && (icounter > bias + 26))
  229.         bias += 26;
  230.     else
  231.         if (c == '\r' && (icounter <= bias + 26))
  232.             bias = 0;
  233.     if (c == '-')
  234.         bias -= 26;
  235.     if (bias < 0)
  236.         bias = 0;
  237.     if (!isalpha(c))
  238.         goto xxxx;
  239.     for (i = 0; toupper(c) != prompt[i]; i++)
  240.         ;
  241.     if (i >= max)
  242.         goto xxxx;
  243.     if (i + bias >= icounter)
  244.         goto xxxx;
  245.     return i + bias;
  246. }
  247.  
  248.  
  249. int string_compare(s, t)
  250. char **s, **t;
  251. {
  252.     char *s1, *t1;
  253.     int i;
  254.     s1 = *s;
  255.     t1 = *t;
  256.     for (i = 0; i < MAXLINE; i++)
  257.     {
  258.         if (t1[i] != s1[i])
  259.             return s1[i] - t1[i];
  260.         if (s1[i] == '\0')
  261.             return 0;
  262.     }
  263.     return 0;
  264. }
  265.  
  266. linecopy(dest, source, argv, argc, bias)
  267. char *dest, *source, **argv;
  268. int argc, bias;
  269. {
  270.     int i;
  271.     char c, tempstr[MAXLINE];
  272.     if (*source == '*')
  273.         *source = ';';
  274.     while (c = *source++)
  275.     {
  276.         if (c == '$' && isdigit(*source))
  277.         {
  278.             i = *source - '0' + bias;
  279.             if (i < argc)
  280.             {
  281.                 strcpy(dest, argv[i]);
  282.                 dest += strlen(argv[i]);
  283.             }
  284.             source++;
  285.         }
  286.         else
  287.             if (c == '$' && *source == '*')
  288.         {
  289.             for (i = 1 + bias; i < argc; i++)
  290.             {
  291.                 if (i < argc)
  292.                 {
  293.                     strcpy(dest, argv[i]);
  294.                     strcat(dest, " ");
  295.                     dest += strlen(argv[i]) + 1;
  296.                 }
  297.             }
  298.             source++;
  299.         }
  300.         else
  301.             if (c == '$' && *source == '#')
  302.         {
  303.             sprintf(tempstr, "%d", argc - bias);
  304.             strcpy(dest, tempstr);
  305.             dest += strlen(tempstr);
  306.             source++;
  307.         }
  308.         else
  309.             if (c == '$' && *source == '$')
  310.         {
  311.             source++;
  312.             *dest++ = c;
  313.         }
  314.         else
  315.             if (c == '^' && isalpha(*source))
  316.         {
  317.             *dest++ = toupper(*source) - 0x40;
  318.             source++;
  319.         }
  320.         else
  321.             {
  322.             *dest++ = c;
  323.         }
  324.     }
  325.     *dest = 0;
  326. }
  327.  
  328.